(function () {

    'use strict';

    angular
        .module('app')
        .controller('PreSalesConfirmController', PreSalesConfirmController);

    PreSalesConfirmController.$inject = ['$scope', '$state', '$rootScope', '$stateParams', 'API', 'PreSalesConfirmService', 'ExpressSelectService', 'PaymentModel'];

    function PreSalesConfirmController($scope, $state, $rootScope, $stateParams, API, PreSalesConfirmService, ExpressSelectService, PaymentModel) {

        $scope.touchAddress = function() {
            $state.go('address-select', {
                address_id: $scope.address ? $scope.address.address_id : null
            });
        };

        $scope.touchProduct = function(part_id) {
            $state.go('pre-sales-info', {
                part_id: part_id
            });
        };

        $scope.touchInvoice = function() {
            $scope.toast('暂不支持在线开票，请联系客服开具发票');
        };

        $scope.touchExpress = function () {
            if (!$scope.address) {
                $scope.toast('请选择地址');
                return;
            }

            var goodsIds = [];
            var numbers = [];
            goodsIds.push($scope.selectedGoods.product.part_id);
            numbers.push($scope.selectedGoods.amount);

            ExpressSelectService.clear();
            ExpressSelectService.expressId = $scope.express ? $scope.express.express_id : null;
            ExpressSelectService.goodsIds = goodsIds;
            ExpressSelectService.goodsNumbers = numbers;
            ExpressSelectService.address_id = $scope.address.address_id;
            ExpressSelectService.type = 'pre';

            $state.go('express-select', {});
        };

        $scope.touchSubmit = _touchSubmit;

        $scope.refreshComment = function (){
            PreSalesConfirmService.input.comment = $scope.input.comment;
        };

        $scope.addList = [];
        $scope.goods = PreSalesConfirmService.product;
        $scope.address = PreSalesConfirmService.address;
        $scope.express = PreSalesConfirmService.express;
        $scope.all_discount = 0;

        $scope.input = {
            comment: ""
        };

        $scope.input.comment = PreSalesConfirmService.input.comment;

        $scope.priceInfo = {};

        $scope.canPurchase = function () {
            if (!$scope.selectedGoods)
                return false;
            if (!$scope.address)
                return false;
            if (!$scope.express)
                return false;

            return true;
        };

        $scope.selectedGoods = {};

        if(PreSalesConfirmService.product){
            $scope.selectedGoods.product = PreSalesConfirmService.product;
            $scope.selectedGoods.amount = 1;//默认购买一份
        }else{
            return;
        }

        _reloadAddress();
        _refreshOrderPrice();

        function _reloadAddress() {
            API.consignee.getAddList({}).then(function (res) {
                if (res) {
                    $scope.addList = res;
                    if (!$scope.address) {
                        for (var key in $scope.addList) {
                            if ($scope.addList[key].is_default) {
                                $scope.address = $scope.addList[key];
                                $rootScope.$emit('consigneeChanged', $scope.address);
                            }
                        }
                    }
                }
                _reloadExpress();
            })
        }


        function _reloadExpress() {
            if (!$scope.express) {
                var goods = {};
                goods.goods_id = $scope.selectedGoods.product.part_id;
                goods.num = $scope.selectedGoods.amount;

                API.shipping.getFirstExpress({
                    goods: JSON.stringify(goods),
                    address_id: $scope.address.address_id,
                    type: 'pre'
                }).then(function (res) {
                    $scope.express = res;
                    _refreshOrderPrice();
                });
            }
        }

        function _refreshOrderPrice() {
            var goods = $scope.goods;
            var express = $scope.express;

            if (!goods) {
                $scope.toast('商品信息不存在');
                return;
            }

            var products = {};
            products.part_id = goods.part_id;
            products.num = 1;

            var params = {};
            params.order_goods = JSON.stringify(products);
            if (express) {
                params.express_region_id = express.express_region_id;
            }

            API.order.getPrePrice(params).then(function (res) {
                $scope.priceInfo = res;
                $scope.all_discount = res.discount_price;
            });
        }

        function _touchSubmit() {

            var address = $scope.address;

            if (!address) {
                $scope.toast('请填写地址');
                return;
            }

            if (!$scope.express) {
                $scope.toast('请选择快递');
                return;
            }

            var params = {};
            params.part_id = $scope.selectedGoods.product.part_id;
            params.amount = $scope.selectedGoods.amount;
            params.leave_words = $scope.input.comment;

            if ($scope.address) {
                params.address_id = $scope.address.address_id;
            }

            if ($scope.express) {
                params.express_region_id = $scope.express.express_region_id;
            }

            API.cart.prePurchase(params).then(function (order) {
                if (order) {
                    PreSalesConfirmService.clear();
                    ExpressSelectService.clear();
                    PaymentModel.clear();
                    $state.go('payment', {
                        order_id: order.order_id
                    });
                }
            });
        }

    }

})();